#encoding:utf-8
'''
Created on 2012/3/6
argv[1]為正確的Label
argv[2]為class的weight
argv[3]為結果
argv[4]為threshold
argv[5]為最多取幾個class
argv[6]是否內部test
@author: Netdb_Heng
'''
#參數一為母體，參數二為輸入Label，參數三為輸出sample，參數四為輸出sample的class，參數五為筆數

import sys

print ( "Class:" + sys.argv[1] + "Class_weight:" + sys.argv[2] + "output:" + sys.argv[3] )

Label_inf = open(sys.argv[1])
Predict_inf = open(sys.argv[2])
if(int(sys.argv[7])==1):
    analysis_outf = open(sys.argv[3],'w')
Label_outf = open(sys.argv[4],'w')
threshold = float(sys.argv[5])
Class_num = int(sys.argv[6])
validate = int(sys.argv[7])



Label_lines = Label_inf.readlines()
Predict_lines = Predict_inf.readlines()

instance=0


def Fscore(Class,Predict):
    same=0
    for i in range(len(Class)):
        for j in range(len(Predict)):
            if(int(Class[i]) == int(Predict[j]) ):
                same+=1
                break
    Precision = float(same) / len(Predict)
    Recall = float(same) / len(Class)
    if(Recall==0 and Precision==0):
        return 0
    else:
        return float(2*Precision*Recall) / (Precision+Recall)
if(validate==1):    
    analysis_outf.write("Prediction \t | \t Answer\n")
    avg_Fscore = 0.0
    for i in range(len(Predict_lines)):
        Class_Weight = Predict_lines[i].split(",")
        Label = Label_lines[i].split(",")
        Class_choose = 0
        Predict = []
        Predict.append(Class_Weight[0].split("=")[0])
        base_Class_Weight = float(Class_Weight[0].split("=")[1])   #最基本的class的weight(第一個)
        for j in range(1,len(Class_Weight) ):
            if(float(Class_Weight[j].split("=")[1]) > (base_Class_Weight*threshold)):
                Predict.append(Class_Weight[j].split("=")[0])
                Class_choose+=1
                if(Class_choose==Class_num-1):
                    break
            else:
                break
        for j in range(len(Predict)):
            if j==0:
                analysis_outf.write(Predict[j])
            else:
                analysis_outf.write("," + Predict[j])
        analysis_outf.write(" \t | \t ")
        for j in range(len(Label)):
            if j==0:
                analysis_outf.write(str(int(Label[j])))
                Label_outf.write(str(int(Label[j])))
            else:
                analysis_outf.write("," + str(int(Label[j])))
                Label_outf.write("," + str(int(Label[j])))
        analysis_outf.write("\t ")
        Fscore_value = Fscore(Label,Predict)
        analysis_outf.write("Fscore:" + str(Fscore_value) + "\n")
        Label_outf.write("\n")
        avg_Fscore += Fscore_value
    avg_Fscore /= len(Predict_lines)
    analysis_outf.write("\n" + "avg_Fscore : "+ str(avg_Fscore))
        
else:
    for i in range(len(Predict_lines)):
        Class_Weight = Predict_lines[i].split(",")
        
        Class_choose = 0
        Predict = []
        Predict.append(Class_Weight[0].split("=")[0])
        base_Class_Weight = float(Class_Weight[0].split("=")[1])   #最基本的class的weight(第一個)
        for j in range(1,len(Class_Weight) ):
            if(threshold==0):
                Predict.append(Class_Weight[j].split("=")[0])
                if(Class_choose==Class_num-1):
                    break
            else:
                if(float(Class_Weight[j].split("=")[1]) > (base_Class_Weight*threshold)):
                    Predict.append(Class_Weight[j].split("=")[0])
                    Class_choose+=1
                    if(Class_choose==Class_num-1):
                        break
                else:
                    break
        for j in range(len(Predict)):
            if j==0:
                Label_outf.write(str(int(Predict[j])) )
            else:
                Label_outf.write("," + str(int(Predict[j])) )
        Label_outf.write("\n")

    
Label_inf.close();
Predict_inf.close();
if(validate==1):
    analysis_outf.close();
Label_outf.close();

